GushiwenDetailPageManager=function(){
	this.inited=false;
	this.appVM=null;
	this.mainScroll=null;
	this.bottomPanel=null;
	this.gushiwenId=null;
	this.gushiwenIdIndex=null;
	this.gushiwenIdList=[];
}

GushiwenDetailPageManager.defValue={
	toGushiwenDetailPageSettingEl:"#gushiwen-det-page-setting",
	toCardMakerPageEl:"#to-card-make-page",
	ilikeItBtnEl:"#i-like-it",
	ilikeItFlagClass:"i-like-it-flag-class",
	gushiwenIdList:[],
	appVMConfig:{
		el:"#gushiwen-info-content",
		data:{
			id:null,
			mainImg:"",
			bgStyle:{},
			title:"",
			dynasty:"",
			writer:"佚名",
			writerId:null,
			content:"",
			shortContent:"",
			tagList:[],
			goodSententceList:[],
			writerInfo:{
				id:null,
				name:"",
				headImageUrl:"../images/defWriterHeader001.jpg",
				simpleIntro:" 暂未收录作者信息",
				dynasty:"",
			}
		},
		methods:{
			onTagClicked:function(e){
				GushiwenDetailPageManager.onTagClicked(this,e);
			},
			onSentenceClicked:function(e){
				GushiwenDetailPageManager.onSentenceClicked(this,e);
			},
			onWriterInfoClicked:function(e){
				GushiwenDetailPageManager.onWriterInfoClicked(this,e);
			}
		}
	},
	mainScrollConfig:{
		el:"#gushiwen-info-content",
		up:{
			enabled:true,//先关掉 底部拉起 floatPanel
			initLoadOnce:false,
			detHeight:50,
			detDelayTime:200,
			moreText:'继续上拉弹出注释层',
			loadingText:'',
			noMoreText:'',
			loadFunction:function(manager){
				var bottomPanel=manager.bottomPanel;
				if(bottomPanel && !$(bottomPanel.option.panelId).is('.'+bottomPanel.popFlagClass)){
					bottomPanel.popUpPanel();
				}
				manager.mainScroll.loadOver(true);
			},
		}
	},
	bottomPanelConfig:{
		panelId:'test-f-p-001',
		tabItemList:[{
			inforId:'detail-zhushi',itemName:'注释',showName:'remark'
		},{
			inforId:'detail-yiwen',itemName:'译文',showName:'translation'
		},{
			inforId:'detail-shangxi',itemName:'赏析',showName:'shangxi'
		}],
	}
}

GushiwenDetailPageManager.prototype.init=function(opts){
	if(!this.inited){
		var that=this;
		this.options=$.extend(true,{},GushiwenDetailPageManager.defValue,opts);
		this.gushiwenId=this.options.gushiwenId;
		this.gushiwenIdList=this.options.gushiwenIdList;
		if(this.gushiwenIdList && this.gushiwenIdList.length>0){
			console.log("has gushiwenIdList")
			for(var index=0;index < this.gushiwenIdList.length;index++){
				if(this.gushiwenIdList[index]==this.gushiwenId){
					this.gushiwenIdIndex=index;
					break;
				}
			}
		}
		//
		this.bottomPanel=new BottomFloatPanel();
		this.bottomPanel.initPanel(this.options.bottomPanelConfig); 
		//
		this.loadDetailInfo(this.gushiwenId);
		this.appVM=new Vue(this.options.appVMConfig);
		//下面这些必须在 vue 初始化后赋值，否则无效
		var mainScrollConfig=this.options.mainScrollConfig;
		var configLoadFunction=mainScrollConfig.up.loadFunction;
		mainScrollConfig.up.loadFunction=function(){
			configLoadFunction(that);
		}
		this.mainScroll=new RefreshAbleScroll(mainScrollConfig.el,mainScrollConfig);
		//
		this.registerEvent();
		this.resizeWindow();
		
	}
	this.inited=true;
}

GushiwenDetailPageManager.prototype.loadDetailInfo=function(id){
	var that=this;
	id=id||this.gushiwenId;
	this.initILikeIt();
	var selVal = SettingInfoManager.getDetPagePeituShowTypeSetting();
	var requestVersion=getRequestVersion();
	var requestData={id:id,version:requestVersion};
	if(selVal){
		requestData.peiTuShowType=selVal;
	}
	var url=getShiciServiceHost()+'/shici/gushiwen/detail';
	muiAjaxGetJsonData({
		showLoading:true,
		url:url,
		data:requestData,
		callback:function(data){
			if(!data.entry){
				return;
			}
			SettingInfoManager.addHistoryInfo(SettingInfoManager.settingName.historyShiwen,id);
			//data.entry.preContent
			replaceNextLine(data.entry,'content',true);
			var appVMConfig=that.options.appVMConfig;
			//不是短句诗文，不使用居中样式
			$(appVMConfig.el).find(".gushiwen-content").css('text-align','center');
			if(data.entry.useCenterStyle==false){
				$(appVMConfig.el).find(".gushiwen-content").css('text-align','inherit');
			}
			//
			replaceNextLine(data.entry,'remark',false);
			replaceNextLine(data.entry,'shangxi',false);
			replaceNextLine(data.entry,'translation',false);
			//
			//data.entry.mainImg='../images/guofeng-banner001.jpg';
			if(data.entry.mainImg){
				data.entry.bgStyle={
					marginTop:'6px',
					borderRadius:'2px',
					//height:'45vw',
					//backgroundImage: 'url('+data.entry.mainImg+')'
				}	
			}
			//appVMConfig
			that.resetAppVMConfigData();
			$.extend(true,appVMConfig.data,data.entry);
			//
			that.bottomPanel.resetFirstTabAndInnerContentTop();
			that.bottomPanel.fillPanelData(data.entry);
			//加载作者简介
			if(data.entry['writerId']){
				that.loadWriterSimpleInfo(data.entry['writerId']);
			}
		},
	});
}

GushiwenDetailPageManager.prototype.resetAppVMConfigData=function(){
	var appVMConfigData=this.options.appVMConfig.data;
	var defData = $.extend(true,{},GushiwenDetailPageManager.defValue.appVMConfig.data); 
	for(var tmpField in appVMConfigData){
		appVMConfigData[tmpField]=defData[tmpField];
		//console.log(tmpField+":"+defData[tmpField]);
	}
	console.log("tagList"+":"+defData['tagList'].length);
}

GushiwenDetailPageManager.prototype.loadWriterSimpleInfo=function(writerId){
	var that=this;
	var writerUrl=getShiciServiceHost()+'/shici/writer/simple';
	muiAjaxGetJsonData({
		url:writerUrl,
		data:{id:writerId},
		callback:function(writerData){
			if(!writerData.entry){
				return;
			}
			var appVMConfig=that.options.appVMConfig;
			appVMConfig.data.writerInfo=writerData.entry;
		}
	});
}

GushiwenDetailPageManager.prototype.registerEvent=function(){
	var that=this;
	var appVMConfig=this.options.appVMConfig;
	var $infoContent=$(appVMConfig.el);
	//
	$(appVMConfig.el).on('swipeleft','.mui-scroll',function(e){
		console.log("swipeleft that.gushiwenIdIndex="+that.gushiwenIdIndex);
		that.reloadShiwenData(true);
	});
	$(appVMConfig.el).on('swiperight','.mui-scroll',function(e){
		that.reloadShiwenData(false);
	});
	//
	$(this.options.toGushiwenDetailPageSettingEl).on('tap',function(e){
		that.toGushiwenDetailPageSetting();
	});
	//
	$(this.options.toCardMakerPageEl).on('tap',function(e){
		that.toCardMakerPage();
	});
	//
	$(this.options.ilikeItBtnEl).on('tap',function(e){
		that.toogleILikeIt();
	});
}

GushiwenDetailPageManager.prototype.resizeWindow=function(){
	var winH=$(window).height();
	var headerH=$('.xq-window-header').height();
	var bodyH=winH-headerH;//-45;
	//$('.xq-window-body').css('padding-bottom',45+'px');
	$('.xq-window-body').css('min-height',bodyH+'px');
	//$('#gushiwen-info-content').css('min-height',bodyH+'px');
}
			//
GushiwenDetailPageManager.prototype.initILikeIt=function(gushiwenId){
	gushiwenId=gushiwenId||this.gushiwenId;
	if(gushiwenId){
		var list=SettingInfoManager.getList(SettingInfoManager.settingName.likeShiwen);
		var index=$.inArray(gushiwenId+'', list);
		var flagClass=this.options.ilikeItFlagClass;
		$(this.options.ilikeItBtnEl).removeClass(flagClass);
		if(index>=0){
			$(this.options.ilikeItBtnEl).addClass(flagClass);
		}
	}
}
//
GushiwenDetailPageManager.prototype.toogleILikeIt=function(){
	var gushiwenId=this.gushiwenId;
	if(gushiwenId){
		var flagClass=this.options.ilikeItFlagClass;
		if($(this.options.ilikeItBtnEl).is('.'+flagClass)){
			SettingInfoManager.removeFromList(SettingInfoManager.settingName.likeShiwen,gushiwenId);
			$(this.options.ilikeItBtnEl).removeClass(flagClass);
		}else{
			SettingInfoManager.addToList(SettingInfoManager.settingName.likeShiwen,gushiwenId,false);
			$(this.options.ilikeItBtnEl).addClass(flagClass);
		}
	}
}

GushiwenDetailPageManager.prototype.toGushiwenDetailPageSetting=function(){
	mui.openWindow({
		url:'settingOfGushiwenDetai.html',
		id:'settingOfGushiwenDetai.html',
		extras:{
		}
	});
}

GushiwenDetailPageManager.prototype.toCardMakerPage=function(){
	var appVMConfigData=this.options.appVMConfig.data;
	var cardContent={
		title:appVMConfigData.title,
		contentText:appVMConfigData.content,
		writer:appVMConfigData.writer
	};
	
	mui.openWindow({
		url:'cardMaker.html?title='+appVMConfigData.title,
		id:'cardMaker.html-title-'+appVMConfigData.title,
		extras:{
			cardContent : cardContent
		}
	});
}

GushiwenDetailPageManager.prototype.reloadShiwenData=function(isLoadNext){
	var that = this;
	var noMoreData=false;
	if(isLoadNext){
		noMoreData=(that.gushiwenIdIndex>=(that.gushiwenIdList.length-1));
	}else{
		noMoreData=(that.gushiwenIdIndex<=0);
	}
	
	if(noMoreData){
		commonUtils.showLoading("暂时没有更多数据！");
		setTimeout(function(){commonUtils.hideLoading(function(){});},600);
		return;
	}
	console.log("swipe that.gushiwenIdIndex="+that.gushiwenIdIndex);
	if(isLoadNext){
		that.gushiwenIdIndex++;
	}else{
		that.gushiwenIdIndex--;
	}
	
	that.gushiwenId=that.gushiwenIdList[that.gushiwenIdIndex];
	
	that.loadDetailInfo(that.gushiwenId);
	
	that.mainScroll.reInit();
}

GushiwenDetailPageManager.onTagClicked=function(that,e){
	var target=e.target;
	var tagIndex=$(target).attr('index');
	var tatInfo=that.$data.tagList[tagIndex];
	var tagId=tatInfo['tagId'];
	var tagName=tatInfo['tagName'];
	if(tagId){
		console.info(tagId);
		mui.openWindow({
			url:'tagGushiwenList.html?tagId='+tagId,
			id:'tagGushiwenList.html-tagId-'+tagId,
			extras:{
				tagId : tagId,
				tagName : tagName
			}
		});
	}
}

GushiwenDetailPageManager.onSentenceClicked=function(that,e){
	var target=e.target;
	var index=$(target).attr('index');
	var goodSententce = that.$data.goodSententceList[index];
	var infoId=goodSententce['id'];
	var sentenceJson='';
	if(goodSententce){
		sentenceJson=JSON.stringify(goodSententce);
	}
	mui.openWindow({
		url: 'sentenceRandomCard.html?id=' + infoId,
		id: 'sentenceRandomCard.html-id-' + infoId,
		extras: {
			infoId: infoId,
			sentence:sentenceJson
		}
	});
}

GushiwenDetailPageManager.onWriterInfoClicked=function(that,e){
	
	var target=e.target;
	var writerId=$(target).closest("[fill-writer-id]").attr('writer-id');
	console.log('writer clicked:writerId='+writerId)
	if(writerId && writerId!=0){
		console.info(writerId);
		mui.openWindow({
			url:'writerDetail.html?writerId='+writerId,
			id:'writerDetail.html-writerId-'+writerId,
			extras:{
				writerId : writerId
			}
		});
	}
}
